/**
 * Created by qunxing.du on 2016/7/12.
 * Copyright 2016 汉威.智慧环保事业部 Limited.
 * All rights reserved.
 */
package com.hw.one.collect.data;

import com.hw.one.collect.util.ConvertUtil;
import com.mongodb.BasicDBList;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * 抽象数据服务
 */
public abstract class AbstractDataService implements IDataService {
    protected static final String TRANSFORM_COLLETCION_SUFFIX ="_TRANS";
    /**
     * json对象数组转成mongo的文档集合
     *
     * @param o
     * @return
     */
    protected synchronized BasicDBList getBasicDBList(Object o) throws Exception{
        //转换JsonArray
        List<Map> list = ConvertUtil.toList(o);
        BasicDBList basicDBList = new BasicDBList();
        if(list != null){
            basicDBList.addAll(list);
        }
        return basicDBList;
    }
    /**
     * 获取自己因子和第三方因子的键值集合
     * @return
     */
    protected synchronized Map<String,String> getFactors(String factors){
        String[] factorsArr = StringUtils.split(factors,",");
        Map<String,String> factorMap = new HashMap<>();
        for (String factor : factorsArr) {
            String[] singleFactorArr = factor.split("=");
            String key = singleFactorArr[0];
            String value = StringUtils.defaultIfBlank(singleFactorArr[1],key);
            factorMap.put(key,value);
        }
        return  factorMap;
    }

    /**
     * 获取自己设备和第三方设备的编码键值集合
     * @return
     */
    protected synchronized Map<String,String> getDevices(String devices){
        String[] devicesArr = StringUtils.split(devices,",");
        Map<String,String> deviceMap = new HashMap<>();
        for (String device : devicesArr) {
            String[] singleDeviceArr = device.split("=");
            String key = singleDeviceArr[0];
            String value = StringUtils.defaultIfBlank(singleDeviceArr[1],key);
            deviceMap.put(key,value);
        }
        return  deviceMap;
    }

    /**
     * 获取小型站小时数据的文档集合名
     * <p>
     * 以半年为单位进行分表,如果当前时间在当前年的 “6月30日 ：23:59:59”之前，则为 “a”，否则为"b"
     * eg: if date =  2017-05-30 23:59:59  && date < 2017-06-30 23:59:59  则返回 T_DATA_SC_HOUR_S_COLLECT-2017a,
     * 否则返回：T_DATA_SC_HOUR_S_COLLECT-2017b
     * </p>
     *
     */
    protected synchronized String getScHourSCollect(Date date){
        Calendar c = Calendar.getInstance();
        String collectName = "T_DATA_AIR_HOUR-";
        String year = String.valueOf(c.get(Calendar.YEAR));

        return collectName+year;
    }

    protected synchronized String getScHourSCollect(){
        return getScHourSCollect(null);
    }
    /**
     * 截止时间线
     * 返回当前年的“6月30日 ：23:59:59”
     * eg:if date =  2017-05-30 23:59:59  return 2017-06-30 23:59:59
     * @return
     */
    protected Date getDeadLine(){
        Calendar c = Calendar.getInstance();
        c.set(Calendar.MONTH,5);
        c.set(Calendar.DAY_OF_MONTH,30);
        c.set(Calendar.HOUR_OF_DAY,c.getMaximum(Calendar.HOUR_OF_DAY));
        c.set(Calendar.MINUTE,c.getMaximum(Calendar.MINUTE));
        c.set(Calendar.SECOND,c.getMaximum(Calendar.SECOND));
        return c.getTime();
    }

}
